# coding: UTF-8
class Admin::ProcessDefinesController < Admin::BaseController

  before_action :base_breadcrumb
  before_action :set_process_define, only: [:show, :edit, :update, :destroy, :create_instance]
  before_action :name_breadcrumb_handler, only: [:show, :edit]
  before_action :action_common_breadcurmb, only: [:new, :edit]

  # GET /admin/process_defines
  def index
    @process_defines = initialize_grid(ProcessDefine, page: params[:page], order: :created_at, order_direction: :desc)
  end

  def create_instance
    process=@process_define.create_instance(current_user.id)
    if process.id
      redirect_to admin_process_instance_path(process.id)
    else
      flash[:notice] = '启动失败'
      redirect_to action: :index
    end
  end

  # GET /admin/process_defines/1
  def show
  end


  # GET /admin/process_defines/new
  def new
    @process_define = ProcessDefine.new
  end

  # GET /admin/process_defines/1/edit
  def edit
  end

  # POST /admin/process_defines
  def create
    @process_define = ProcessDefine.new(process_define_params)

    respond_to do |format|
      if @process_define.save
        format.html { redirect_to admin_process_define_path(@process_define.id), notice: create_success_notice(:process_define) }
        format.json { render action: 'show', status: :created, location: @process_define }
      else
        format.html { render action: 'new' }
        format.json { render json: @process_define.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /admin/process_defines/1
  def update
    respond_to do |format|
      if @process_define.update_attributes(process_define_params)
        format.html { redirect_to admin_process_define_path(@process_define.id), notice: update_success_notice(:process_define) }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @process_define.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /admin/process_defines/1
  def destroy
    begin
      @process_define.destroy
    rescue
      @process_define.update_attributes(enable: false)
    end

    respond_to do |format|
      format.html { redirect_to admin_process_defines_path, notice: destroy_success_notice(:process_define) }
      format.json { head :no_content }
    end
  end

  def destroy_multi
    ids =params[:grid][:select_ids]
    if ids and ids.instance_of? Array
      ids.each do |id|
        process_define = ProcessDefine.find(id)
        begin
          process_define.destroy! if process_define
        rescue ActiveRecord::RecordNotDestroyed
          process_define.update_attributes(enable: false)
          next
        end
      end
    end
  rescue
    flash[:notice] ="#{t 'common.delete_multi'}#{t 'common.fault'}"
    redirect_to action: 'index'
  else
    flash[:notice] ="#{t 'common.delete_multi'}#{t 'common.success'}"
    redirect_to action: 'index'
  end


  private
  # Use callbacks to share common setup or constraints between actions.
  def set_process_define
    if params[:action] == 'show'
      @process_define = ProcessDefine.find(params[:id])
    else
      @process_define = ProcessDefine.find(params[:id])
    end
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def process_define_params

    params.require(:process_define).permit(:bpmn_id, :bpmn_key, :current_version_code)

  end

  def base_breadcrumb
    add_breadcrumb (t 'activerecord.attributes.process_define.admin_title'), admin_process_defines_path
  end

  # add name breadcrumb with :show, :edit
  def name_breadcrumb_handler
    add_breadcrumb @process_define.id, admin_process_define_path(@process_define.id), class: :pjax
  end
end